var TT = TAOTAO = {
	// 编辑器参数
	kingEditorParams : {
		//指定上传文件参数名称
		filePostName  : "uploadFile",
		//指定上传文件请求的url。
		uploadJson : '/pic/upload',
		//上传类型，分别为image、flash、media、file
		dir : "image",
        afterBlur: function(){this.sync();},
        width : '700px',
        height : '300px',
	},
    createEditor : function(select){
        return KindEditor.create(select, TT.kingEditorParams);
    },
    init : function(data){
        // 初始化多张图片上传组件
        this.initPicUpload(data);
        // 初始化选择类目组件
        this.initItemCat(data);
        //初始化用户选择组件
        this.initSelectUser(data);
        // 初始化单张图片上传组件
        this.initOnePicUpload();
        // 初始化单张图片上传组件 身份证正面
        this.initOnePicUpload1();
        // 初始化单张图片上传组件 身份证背面
        this.initOnePicUpload2();
        // 初始化单个文件上传组件
        this.initOneFileUpload();
    },

    // 初始化图片上传组件
    initPicUpload : function(data){
        $(".picFileUpload").each(function(i,e){
            var _ele = $(e);
            _ele.siblings("div.pics").remove();
            _ele.after('\
    			<div class="pics">\
        			<ul></ul>\
        		</div>');
            // 回显图片
            if(data && data.pics){
                var imgs = data.pics.split(",");
                for(var i in imgs){
                    if($.trim(imgs[i]).length > 0){
                        _ele.siblings(".pics").find("ul").append("<li><a href='"+imgs[i]+"' target='_blank'><img src='"+imgs[i]+"' width='90' height='90' /></a></li>");
                    }
                }
            }
            //给“上传图片按钮”绑定click事件
            $(e).click(function(){
                var form = $(this).parentsUntil("form").parent("form");
                //打开图片上传窗口
                KindEditor.editor(TT.kingEditorParams).loadPlugin('multiimage',function(){
                    var editor = this;
                    editor.plugin.multiImageDialog({
                        clickFn : function(urlList) {
                            var imgArray = [];
                            KindEditor.each(urlList, function(i, data) {
                                imgArray.push(data.url);
                                form.find(".pics ul").append("<li><a href='"+data.url+"' target='_blank'><img src='"+data.url+"' width='90' height='90' /></a></li>");
                                //原先图片销毁
                                $("#img").empty();
                            });
                            //给数据库字段赋值
                            form.find("[name=imgUrl]").val(imgArray.join(","));
                            editor.hideDialog();
                        }
                    });
                });
            });
        });
    },

    // 初始化选择类目组件
    initItemCat : function(data){
        $(".selectItemCat").each(function(i,e){
            var _ele = $(e);
            if(data && data.itemCatId){
                _ele.after("<span style='margin-left:10px;'>"+data.itemCatId+"</span>");
            }else{
                _ele.after("<span style='margin-left:10px;'></span>");
            }
            _ele.unbind('click').click(function(){
                $("<div>").css({padding:"5px"}).html("<ul>")
                    .window({
                        width:'500',
                        height:"450",
                        modal:true,
                        closed:true,
                        iconCls:'icon-save',
                        title:'选择类目',
                        onOpen : function(){
                            var _win = this;
                            $("ul",_win).tree({
                                url:'/item/category/categoryList',
                                animate:true,
                                onClick : function(node){
                                    if($(this).tree("isLeaf",node.target)){
                                        // 填写到cid中
                                        _ele.parent().find("[name=itemCatId]").val(node.id);
                                        _ele.next().text(node.text).attr("itemCatId",node.id);
                                        _ele.parent().find("[name=name]").val(node.text);
                                        _ele.next().text(node.text).attr("name",node.text);

                                        _ele.parent().find("[name=itemCatName]").val(node.text);
                                        _ele.next().text(node.text).attr("itemCatName",node.text);

                                        $(_win).window('close');
                                        if(data && data.fun){
                                            data.fun.call(this,node);
                                        }
                                    }
                                }
                            });
                        },
                        onClose : function(){
                            $(this).window("destroy");
                        }
                    }).window('open');
            });
        });
    },
    // 初始化选择类目组件
    initSelectUser : function(data){
        $(".selectUser").each(function(i,e){
            var _ele = $(e);
            if(data && data.cid){
                _ele.after("<span style='margin-left:10px;'>"+data.cid+"</span>");
            }else{
                _ele.after("<span style='margin-left:10px;'></span>");
            }
            _ele.unbind('click').click(function(){
                $("<div>").css({padding:"5px"}).html("<ul>")
                    .window({
                        width:'500',
                        height:"450",
                        modal:true,
                        closed:true,
                        iconCls:'icon-save',
                        title:'选择类目',
                        onOpen : function(){
                            var _win = this;
                            $("ul",_win).tree({
                                url:'/data/category/adminList',
                                animate:true,
                                onClick : function(node){
                                    if($(this).tree("isLeaf",node.target)){
                                        // 填写到cid中
                                        _ele.parent().find("[name=categoryId]").val(node.id);
                                        _ele.next().text(node.text).attr("categoryId",node.id);
                                        _ele.parent().find("[name=categoryName]").val(node.text);
                                        _ele.next().text(node.text).attr("categoryName",node.text);
                                        $(_win).window('close');
                                        if(data && data.fun){
                                            data.fun.call(this,node);
                                        }
                                    }
                                }
                            });
                        },
                        onClose : function(){
                            $(this).window("destroy");
                        }
                    }).window('open');
            });
        });
    },

    /**
     * 初始化单图片上传组件 <br/>
     * 选择器为：.onePicUpload <br/>
     * 上传完成后会设置input内容以及在input后面追加<img>
     */
    initOnePicUpload : function() {
        $(".onePicUpload").click(function () {
            var _self = $(this);
            KindEditor.editor(TT.kingEditorParams).loadPlugin('image', function () {
                this.plugin.imageDialog({
                    showRemote: true,
                    clickFn: function (url, title, width, height, border, align) {
                        _self.parent().find("[name=url]").val(url);

                        var input = _self.siblings("input");
                        input.parent().find("img").remove();
                        input.val(url);
                        //$(".img_url").val(url);
                        input.after("<a href='" + url + "' target='_blank'><img src='" + url + "'  height='90'/></a>");
                        //input.replaceWith("<a href='"+url+"' target='_blank'><img src='"+url+"' width='90' height='90'/></a>");
                        this.hideDialog();
                    }
                });
            });
        });
    },
    /**
     * 初始化单图片上传组件 <br/>
     * 身份证正面照
     * 选择器为：.onePicUpload <br/>
     * 上传完成后会设置input内容以及在input后面追加<img>
     */
    initOnePicUpload1 : function() {
        $(".onePicUpload1").click(function () {
            var _self = $(this);
            KindEditor.editor(TT.kingEditorParams).loadPlugin('image', function () {
                this.plugin.imageDialog({
                    showRemote: true,
                    clickFn: function (url, title, width, height, border, align) {
                        _self.parent().find("[name=idCard1]").val(url);

                        var input = _self.siblings("input");
                        input.parent().find("img").remove();
                        input.val(url);
                        //$(".img_url").val(url);
                        input.after("<a href='" + url + "' target='_blank'><img src='" + url + "'  height='90'/></a>");
                        //input.replaceWith("<a href='"+url+"' target='_blank'><img src='"+url+"' width='90' height='90'/></a>");
                        this.hideDialog();
                    }
                });
            });
        });
    },
    /**
     * 初始化单图片上传组件 <br/>
     * 身份证背面照
     * 选择器为：.onePicUpload <br/>
     * 上传完成后会设置input内容以及在input后面追加<img>
     */
    initOnePicUpload2 : function() {
        $(".onePicUpload2").click(function () {
            var _self = $(this);
            KindEditor.editor(TT.kingEditorParams).loadPlugin('image', function () {
                this.plugin.imageDialog({
                    showRemote: true,
                    clickFn: function (url, title, width, height, border, align) {
                        _self.parent().find("[name=idCard2]").val(url);

                        var input = _self.siblings("input");
                        input.parent().find("img").remove();
                        input.val(url);
                        //$(".img_url").val(url);
                        input.after("<a href='" + url + "' target='_blank'><img src='" + url + "'  height='90'/></a>");
                        //input.replaceWith("<a href='"+url+"' target='_blank'><img src='"+url+"' width='90' height='90'/></a>");
                        this.hideDialog();
                    }
                });
            });
        });
    },
    initOneFileUpload :function(){
        $(".oneFileUpload").click(function () {
            var _self = $(this);
            KindEditor.editor(TT.kingEditorParams).loadPlugin('insertfile', function () {
                    this.plugin.fileDialog({
                        clickFn : function(url, fileName) {
                            var urls = _self.siblings("input[name='urls']");
                            var names = _self.siblings("input[name='names']");
                            if (urls.val()=="") urls.val(url);
                            else  urls.val(urls.val()+","+url);
                            if (names.val()=="") names.val(fileName);
                            else  names.val(names.val()+","+fileName);
                            this.hideDialog();
                        }
                    });


            })
        })
    }
};
